﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace proRFID_DiemDanh.Chung
{
    public class mdMain
    {
        public static SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["chuoiketnoi"].ConnectionString);

        //mở kết nối với database
        public static void OpenConnection()
        {
            if (cnn.State == System.Data.ConnectionState.Closed)
                cnn.Open();
        }

        //đóng kết nối với database
        public static void CloseConnection()
        {
            if (cnn.State == System.Data.ConnectionState.Open)
                cnn.Close();
        }

        public static void OKBox(Page a, string message, string pagetranfer)
        {
            ClientScriptManager CSM = a.ClientScript;
            string strconfirm = "<script>if(!alert('" + message + "')){window.location.href='" + pagetranfer +"'}</script>";
            CSM.RegisterClientScriptBlock(a.GetType(), "Confirm", strconfirm, false);
        }

        public static void OKCancelBox(Page a, string message, string pagetranfer)
        {
            ClientScriptManager CSM = a.ClientScript;
            string strconfirm = "<script>if(!window.confirm('" + message + "')){window.location.href='" + pagetranfer + "'}</script>";
            CSM.RegisterClientScriptBlock(a.GetType(), "Confirm", strconfirm, false);
        }

    }

    public class WebMsgBox
    {
        protected static Hashtable handlerPages = new Hashtable();
        private WebMsgBox()
        {
        }
        public static void Show(string Message)
        {
            if (!(handlerPages.Contains(HttpContext.Current.Handler)))
            {
                Page currentPage = (Page)HttpContext.Current.Handler;
                if (!((currentPage == null)))
                {
                    Queue messageQueue = new Queue();
                    messageQueue.Enqueue(Message);
                    handlerPages.Add(HttpContext.Current.Handler, messageQueue);
                    currentPage.Unload += new EventHandler(CurrentPageUnload);
                }
            }
            else
            {
                Queue queue = ((Queue)(handlerPages[HttpContext.Current.Handler]));
                queue.Enqueue(Message);
            }
        }
        private static void CurrentPageUnload(object sender, EventArgs e)
        {
            Queue queue = ((Queue)(handlerPages[HttpContext.Current.Handler]));
            if (queue != null)
            {
                StringBuilder builder = new StringBuilder();
                int iMsgCount = queue.Count;
                builder.Append("<script language='javascript'>");
                string sMsg;
                while ((iMsgCount > 0))
                {
                    iMsgCount = iMsgCount - 1;
                    sMsg = System.Convert.ToString(queue.Dequeue());
                    sMsg = sMsg.Replace("\"", "'");
                    builder.Append("alert( \"" + sMsg + "\" );");
                }
                builder.Append("</script>");
                handlerPages.Remove(HttpContext.Current.Handler);
                HttpContext.Current.Response.Write(builder.ToString());
            }
        }
    }
    
}